/*
 * Copyright (C) 2011 mAPPn.Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mol.market.common.widget;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mol.market.R;
import com.mol.market.Session;
import com.mol.market.common.util.ThemeManager;

/**
 * 标题导航栏
 * 
 * @author pentiumao
 * @date   2011-12-13
 * @since  Version 0.9
 *
 */
public class NavigationTitle extends LinearLayout {

    private int mCurrentLevel = 0;

    private ArrayList<TextView> mNavigationTitles = new ArrayList<TextView>();
    
    public NavigationTitle(Context context) {
        super(context);
    }
    
    public NavigationTitle(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void pushTitle(String title) {

        TextView v = createTextView(getContext(), title, mCurrentLevel);

        // 如果是多级目录，改变上级的字体样式
        if (mCurrentLevel > 0) {
            for (int i = 0, length = mNavigationTitles.size(); i < length; i++) {
                TextView lastTitle = mNavigationTitles.get(i);
                lastTitle.setTextAppearance(getContext(), R.style.text_style_3d);
            }
        }
        addView(v, mCurrentLevel++);
        mNavigationTitles.add(v);
    }
    
    public void popTitle() {

        if (mCurrentLevel <= 1) {
            return;
        }

        mNavigationTitles.remove(mCurrentLevel - 1);
        removeViewAt(mCurrentLevel - 1);

        // 修改上一级菜单字体样式为当前层级
        TextView lastTitle = mNavigationTitles.get(mCurrentLevel - 2);
        lastTitle.setTextAppearance(getContext(), ThemeManager.getResource(
                Session.get(getContext()), ThemeManager.KEY_TOP_TITLE_TEXT));
        mCurrentLevel--;
    }
    
    /*
     * 换皮肤
     */
    public void initSkin(int theme) {

        if (mNavigationTitles.size() <= 0) {
            return;
        }
        
        TextView currentTitle = mNavigationTitles.get(mCurrentLevel - 1);
        currentTitle.setTextAppearance(getContext(), ThemeManager.getResource(
                Session.get(getContext()), ThemeManager.KEY_TOP_TITLE_TEXT));
        if (theme == ThemeManager.THEME_TYPE_DARK) {
            currentTitle.setShadowLayer(0.1f, 0, -2.0f, Color.BLACK);
        } else {
            currentTitle.setShadowLayer(0f, 0f, 0f, Color.WHITE);
        }

        if (mCurrentLevel > 0) {
            for (int i = 0, length = mNavigationTitles.size() - 1; i < length; i++) {
                TextView lastTitle = mNavigationTitles.get(i);
                lastTitle.setTextAppearance(getContext(), R.style.text_style_3d);
                if (theme == ThemeManager.THEME_TYPE_DARK) {
                    lastTitle.setShadowLayer(0.1f, 0, -2.0f, Color.BLACK);
                } else {
                    lastTitle.setShadowLayer(0f, 0f, 0f, Color.WHITE);
                }
            }
        }
    }
    
    /*
     * 创建导航层View
     */

    private TextView createTextView(Context context, String text, int level) {

        TextView v = (TextView) LayoutInflater.from(context).inflate(R.layout.topbar_title, this,
                false);
        v.setIncludeFontPadding(false);
        v.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
        v.setGravity(Gravity.CENTER);
        v.setTextAppearance(context, ThemeManager.getResource(
                Session.get(getContext()), ThemeManager.KEY_TOP_TITLE_TEXT));
        if (Session.get(getContext()).getTheme() == ThemeManager.THEME_TYPE_DARK) {
            v.setShadowLayer(0.1f, 0, -2.0f, Color.BLACK);
        } else {
            v.setShadowLayer(0f, 0f, 0f, Color.WHITE);
        }
        v.setSingleLine(true);
        v.setText(text);
        if (level != 0) {
            v.setCompoundDrawablesWithIntrinsicBounds(ThemeManager.getResource(
                    Session.get(getContext()), ThemeManager.KEY_TOP_BAR_SPLITTER), 0, 0, 0);
        }
        return v;
    }

}
